import { Controller } from "../../mvc/core/Controller";
import { DropItemWindow } from "../view/dropItem/DropItemWindow";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { DataEvent } from "../events/DataEvent";
import { DropItemEntity } from "../view/dropItem/DropItemEntity";
import { ThingUtil } from "../scene/layer/utils/ThingUtil";
import { DropItemPlayer } from "../scene/player/item/DropItemPlayer";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { DropItemData } from "../view/dropItem/DropItemData";
import { ItemConfig } from "../resource/ItemConfig";
import { ECategory } from "../../../Message/Public/ECategory";
import { EProp } from "../../../Message/Public/EProp";
import { ItemsUtil } from "../view/shortcuts/item/ItemsUtil";
import { LoaderManager } from "../../../com/gengine/resource/LoaderManager";
import { LoaderPriority } from "../../../com/gengine/resource/LoaderPriority";
import { ImageInfo } from "../../../com/gengine/resource/info/ImageInfo";
import { FlyToNavbarTool } from "../view/award/FlyToNavbarTool";
import { IView } from "../../mvc/interfaces/IView";
import { GameProxy } from "../mvc/GameProxy";
import { SystemSetter } from "../view/system/SystemSetter";
type int = number;
//class DropItemController
    
    export  class DropItemController extends Controller
    {
       

        /* internal  */get dropItem(): DropItemWindow
        {
            if (this._dropItem == null) 
            {
                this.initView();
            }
            return this._dropItem;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.AutoPathOpenDropItem, this.onDropItemHandler);
            Dispatcher.addEventListener(EventName.Pick_Item, this.onPickItemHandler);
            Dispatcher.addEventListener(EventName.Pick_AllItem, this.onPickAllItemHandler);
            Dispatcher.addEventListener(EventName.SpaceDropItem, this.onSpaceDropItemHandler);
            Dispatcher.addEventListener(EventName.AutoPick_Item, this.onAutoPickItemHandler);
            return;
        }

        /* internal  */onAutoPickItemHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as DropItemEntity;
            var loc2=ThingUtil.dropItemUtil.getDropItem(loc1.entityID);
            if (loc2) 
            {
                if (cache.role.entityInfo.level >= 30) 
                {
                    this.autoPickItemNotOpenWindow(loc2);
                }
                else 
                {
                    this.dropItem.dropItemPlayer = loc2;
                    this.dropItem.show();
                    flash.utils.setTimeout(this.updateDropItem, 500, loc1);
                }
            }
            return;
        }

        /* internal  */autoPickItemNotOpenWindow(arg1: DropItemPlayer): void
        {
            var loc1=null;
            var loc4=null;
            var loc2=RolePlayer.instance.stagePointX;
            var loc3=RolePlayer.instance.stagePointY;
            var loc5=arg1.dropItemInfo.getArray();
            var loc6=0;
            var loc7=loc5;
            for(loc4 of loc7) 
            {
                loc1 = new Object /* flash.geom.Point */(loc2, loc3);
                loc3 = loc3 + 30;
                this.flyDropItemToBackPack(loc4, loc1);
            }
            return;
        }

        /* internal  */flyDropItemToBackPack(arg1: DropItemData, arg2: Object /* flash.geom.Point */): void
        {
            var loc1=null;
            var loc2=null;
            if (arg1) 
            {
                loc1 = ItemConfig.instance.getInfoByCode(arg1.itemCode);
                if (loc1) 
                {
                    loc2 = loc1.url;
                    if (loc1.item.category == ECategory._ECategoryProp && loc1.item.type == EProp._EPropCoinBindSundry) 
                    {
                        loc2 = ItemsUtil.CoinIcoUrl;
                    }
                    LoaderManager.instance.load(loc2, this.onLoaded, LoaderPriority.LevelD, arg2);
                }
            }
            return;
        }

        /* internal  */onLoaded(arg1: ImageInfo): void
        {
            FlyToNavbarTool.flyToBackPack(arg1.bitmapData, arg1.extData as Object /* flash.geom.Point */);
            return;
        }

        /* internal  */updateDropItem(arg1: DropItemEntity): void
        {
            this.dropItem.autoPickItem(arg1.data as Map<any, any> /* flash.utils.Dictionary */);
            return;
        }

        protected /* override */ initView(): IView
        {
            this._dropItem = new DropItemWindow();
            return this._dropItem;
        }

        /* internal  */onSpaceDropItemHandler(arg1: DataEvent): void
        {
            var loc1=null;
            if (this.dropItem.isHide) 
            {
                loc1 = ThingUtil.dropItemUtil.getRandomDropItem();
                if (loc1) 
                {
                    this.dropItem.dropItemPlayer = loc1;
                    this.dropItem.show();
                }
            }
            else 
            {
                this.dropItem.pickServerAllItem();
            }
            return;
        }

        /* internal  */onPickAllItemHandler(arg1: DataEvent): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=null;
            var loc4=null;
            if (arg1.isResult != false) 
            {
                if ((loc3 = arg1.data as DropItemEntity).isAuto) 
                {
                    loc4 = ThingUtil.dropItemUtil.getDropItem(loc3.entityID);
                    this.dropItem.dropItemPlayer = loc4;
                    this.dropItem.show();
                    flash.utils.setTimeout(this.dropItem.pickAllItem, 500);
                }
                else 
                {
                    this.dropItem.pickAllItem();
                }
            }
            else 
            {
                loc2 = arg1.data as Array<any>;
                if (loc2 && loc2.length > 0) 
                {
                    loc1 = loc2[0] as DropItemData;
                    GameProxy.sceneProxy.pickItem(loc1.entityID, loc2);
                }
            }
            return;
        }

        /* internal  */onPickItemHandler(arg1: DataEvent): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=null;
            if (arg1.isResult != false) 
            {
                loc2 = arg1.data as DropItemEntity;
                loc3 = ThingUtil.dropItemUtil.getDropItem(loc2.entityID);
                this.dropItem.dropItemPlayer = loc3;
                this.dropItem.pickItem(loc2.data as DropItemData);
            }
            else 
            {
                loc1 = arg1.data as DropItemData;
                if (loc1) 
                {
                    GameProxy.sceneProxy.pickItem(loc1.entityID, loc1);
                }
            }
            return;
        }

        /* internal  */onDropItemHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as DropItemPlayer;
            if (loc1) 
            {
                this.dropItem.dropItemPlayer = loc1;
                this.dropItem.show();
                if (this.isAutoPick && loc1.dropItemInfo.packet) 
                {
                    GameProxy.sceneProxy.pickItem(loc1.dropItemInfo.packet.entityId, loc1.dropItemInfo.getArray());
                }
            }
            return;
        }

        public get isAutoPick(): Boolean
        {
            return SystemSetter.currentSetter.clickToPick;
        }

        private /* var */_dropItem: DropItemWindow;

        private /* var */_timer: Object /* flash.utils.Timer */;

        private /* var */_type: int;
    }
